package com.example.soa_exp2.Controller;

import com.example.soa_exp2.Config.CommonResponse;
import com.example.soa_exp2.Domain.Student;
import com.example.soa_exp2.Service.StudentService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
@Tag (name = "学生信息管理模块")
public class StudentController {
    @Resource
    public StudentService studentService;

    @Operation(summary = "新增学生")
    @PostMapping
    public CommonResponse<Student> addStudent(@RequestBody Student stu) {
        if(stu.getNum()==null){
            return CommonResponse.BackForError("学号为空，新增学生失败");
        } else if (stu.getName()==null) {
            return CommonResponse.BackForError("姓名为空，新增学生失败");
        } else if (stu.getTel()==null) {
            return CommonResponse.BackForError("联系电话为空，新增学生失败");
        }else{
            int result = studentService.addStudent(stu);
            if(result == 0){
                return CommonResponse.BackForError("新增学生失败");
            }else {
                return CommonResponse.BackForSuccess("新增学生成功","http://127.0.0.1:8081/student");
            }
        }
    }

    @Operation(summary = "修改学生信息")
    @PutMapping
    public CommonResponse<Student> updateStudent(@RequestBody Student stu){
        if(stu.getNum()==null){
            return CommonResponse.BackForError("学号为空，修改学生信息失败");
        } else if (stu.getName()==null) {
            return CommonResponse.BackForError("姓名为空，修改学生信息失败");
        } else if (stu.getTel()==null) {
            return CommonResponse.BackForError("联系电话为空，修改学生信息失败");
        }else{
            int result = studentService.updateStudentById(stu);
            if(result == 0){
                return CommonResponse.BackForError("修改学生信息失败");
            }else {
                return CommonResponse.BackForSuccess("修改学生信息成功","http://127.0.0.1:8081/student");
            }
        }
    }

    @Operation(summary = "删除学生信息")
    @DeleteMapping
    public CommonResponse<Student> deleteStudentById(String stuId){
        int result = studentService.deleteStudentById(stuId);
        if(result == 0){
            return CommonResponse.BackForError("删除学生信息失败");
        }else{
            return CommonResponse.BackForSuccess("删除学生信息成功","http://127.0.0.1:8081/student");
        }
    }

    @Operation(summary = "通过学号搜索学生信息")
    @GetMapping("/byId")
    public CommonResponse<Student> getStudentById(@RequestParam("stuId") String stuId){
        Student stu = studentService.getStudentByNum(stuId);
        if (stu == null) {
            return CommonResponse.BackForError("未搜索到相关学生信息");
        }else {
            return CommonResponse.BackForSuccess("通过学号搜索学生信息成功", stu,"http://127.0.0.1:8081/student");
        }
    }

    @Operation(summary = "通过姓名搜索学生信息")
    @GetMapping("/byName")
    public CommonResponse<List<Student>> getStudentByName(@RequestParam("name") String name){
        List<Student> students = studentService.getStudentByName(name);
        if(students == null){
            return CommonResponse.BackForError("未搜索到相关学生信息");
        }else {
            return CommonResponse.BackForSuccess("通过姓名搜索学生信息成功", students,"http://127.0.0.1:8081/student");
        }
    }
}
